package com.googlecode.animator.view.timeline
{
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	/**
	 * The base class for any marker to appear on the timeline. 
	 * @author USER
	 * 
	 */
	public class Marker extends SnapDraggableObject
	{
		private var _object3DName:String;
		private var _delete:ContextMenuItem;
		private var _deleteAll:ContextMenuItem;
		private var _copy:ContextMenuItem;
		private var _paste:ContextMenuItem;
		private var _customContextMenu:ContextMenu;
		
		public static const MARKER_DELETE:String = "markerDelete";
		public static const MARKER_DELETE_ALL:String = "markerDeleteAll";
		public static const MARKER_COPY:String = "markerCopy";
		public static const MARKER_PASTE:String = "markerPaste";
		
		/**
		 * Construcor. 
		 * @param w
		 * 
		 */
		public function Marker(w:uint)
		{
			super(w);
			
			_customContextMenu = new ContextMenu();
			_customContextMenu.hideBuiltInItems();
			
			addDeleteMenuItem();
			addDeleteAllMenuItem();
			addCopyMenuItem();
			addPasteMenuItem();

			contextMenu = _customContextMenu;
	
		}
		
		protected function addDeleteMenuItem():void
		{
			_delete = new ContextMenuItem("Delete Marker");
			_delete.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onMarkerDelete);
			_customContextMenu.customItems.push(_delete);
		}
		
		protected function addDeleteAllMenuItem():void
		{
			_deleteAll = new ContextMenuItem("Delete All Markers");
			_deleteAll.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onMarkerDeleteAll);
			
			_customContextMenu.customItems.push(_deleteAll);
		}
		
		protected function addCopyMenuItem():void
		{
			_copy = new ContextMenuItem("Copy Marker");
			_copy.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onMarkerCopy);
			
			_customContextMenu.customItems.push(_copy);
		}
		
		protected function addPasteMenuItem():void
		{
			_paste = new ContextMenuItem("Paste Marker");
			_paste.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onMarkerPaste);

			_customContextMenu.customItems.push(_paste);
		}
		
		protected function onMarkerDelete(evt:ContextMenuEvent):void
		{
			dispatchEvent(new Event(MARKER_DELETE, true));
		}
		
		protected function onMarkerDeleteAll(evt:ContextMenuEvent):void
		{
			dispatchEvent(new Event(MARKER_DELETE_ALL, true));
		}
		
		protected function onMarkerCopy(evt:ContextMenuEvent):void
		{
			dispatchEvent(new Event(MARKER_COPY, true));
		}
		
		protected function onMarkerPaste(evt:ContextMenuEvent):void
		{
			dispatchEvent(new Event(MARKER_PASTE, true));
		}
		
		/**
		 * All markers are related to an object3D. object3DName stores a reference to the object3D. 
		 * @param name
		 * 
		 */
		public function set object3DName(name:String):void
		{
			_object3DName = name;
		}
		
		public function get object3DName():String
		{
			return _object3DName;
		}
		
		/**
		 * Clean up. 
		 * 
		 */
		override public function cleanUp():void
		{
			super.cleanUp();
			
			_delete.removeEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onMarkerDelete);
			
			_deleteAll.removeEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onMarkerDeleteAll);
			
			_copy.removeEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onMarkerCopy);
			
			_paste.removeEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onMarkerPaste);
		}
		
		
	}
}